package besta.moon.sql.expression.function;

import besta.moon.sql.datatype.MutableNumeric;
import besta.moon.sql.parser.SQLTokenizer;
import besta.moon.util.MoonUtils;

public abstract class FunctionReturnFloat extends ExpressionFunction {

    @Override
    public boolean isNull() throws Exception {
        return param1.isNull();
    }

    @Override
    public final boolean getBoolean() throws Exception {
        return getDouble() != 0;
    }

    @Override
    public final int getInt() throws Exception {
        return (int) getDouble();
    }

    @Override
    public final long getLong() throws Exception {
        return (long) getDouble();
    }

    @Override
    public final float getFloat() throws Exception {
        return (float) getDouble();
    }

    @Override
    public long getMoney() throws Exception {
        return MoonUtils.doubleToMoney(getDouble());
    }

    @Override
    public final MutableNumeric getNumeric() throws Exception {
        if (isNull()) {
            return null;
        }
        double value = getDouble();
        if (Double.isInfinite(value) || Double.isNaN(value)) {
            return null;
        }
        return new MutableNumeric(value);
    }

    @Override
    public final Object getObject() throws Exception {
        if (isNull()) {
            return null;
        }
        return getDouble();
    }

    @Override
    public final String getString() throws Exception {
        Object obj = getObject();
        if (obj == null) {
            return null;
        }
        return obj.toString();
    }

    @Override
    public final int getDataType() {
        return SQLTokenizer.FLOAT;
    }

}
